//
//  MTOpenRedpackHistoryHeaderView.swift
//  MT
//
//  Created by ooops on 2019/12/24.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory

class MTOpenRedpackHistoryHeaderView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        makeConstraint()
    }
    
    private func setupUI() {
        addSubview(avatorImgView)
        addSubview(fromUserNameLabel)
        addSubview(redpackMoneyCount)
    }
    
    private func makeConstraint() {
        avatorImgView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(16.auto())
            make.width.height.equalTo(44.auto())
        }
        
        fromUserNameLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(avatorImgView.snp.bottom).offset(8.auto())
        }
        
        redpackMoneyCount.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview().offset(18)
            make.top.equalTo(fromUserNameLabel.snp.bottom).offset(15.auto())
        }
    }
    
    var openRedpackModel: MTQueryRedpackModel? {
        didSet {
            if openRedpackModel != nil {
                if let avator = JMSGUser.myInfo().avatar {
                    avatorImgView.mtSetImage(avator, nil)
                }
                
                if let nickName = UserDefaults.MTUserCenter.nickname.storedString {
                    fromUserNameLabel.text = "\(nickName)共\(openRedpackModel?.isSender == true ? "发出" : "收到")"
                }
                
                if let result = openRedpackModel?.money {
                    redpackMoneyCount.attributedText = attributePullMoney("\(result)")
                }
                
            }
        }
    }
    
    private func attributePullMoney(_ moneyString: String) -> NSAttributedString {
        
        let attributeText: [NSAttributedString.Key : Any] = [.font : PingFangSCRegularFont(14), .foregroundColor : UIColor.redpack.xef1e66]
        
        let attributeString = NSMutableAttributedString(string: "\(moneyString)点券", attributes: attributeText)
        
        let attributeNum: [NSAttributedString.Key : Any] = [.font : PingFangSCSemiboldFont(35), .foregroundColor : UIColor.redpack.xef1e66]
        
        let attributeNumRange: NSRange = NSRange(location: 0, length: moneyString.count)
        
        attributeString.addAttributes(attributeNum, range: attributeNumRange)
        
        return attributeString
    }
    
    lazy var fromUserNameLabel: UILabel = {
        let l = UILabel()
        l.textColor = UIColor(hex: 0xFFFFFF)
        l.font = PingFangSCRegularFont(14.auto())
        l.text = ""
        l.textAlignment = .center
        return l
    }()
    
    lazy var avatorImgView: UIImageView = {
        let imgV = UIImageView()
        imgV.layer.cornerRadius = 22.auto()
        imgV.layer.masksToBounds = true
        imgV.backgroundColor = .white
        imgV.contentMode = .scaleAspectFill
        return imgV
    }()
    
    lazy var redpackMoneyCount: UILabel = {
        let l = UILabel()
        l.textColor = UIColor.redpack.xef1e66
        l.text = ""
        return l
    }()
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }


}
